<template>
	<view class="parent-view">
		<scroll-view class="scroll-view" scroll-y="true">
			<view class="list" v-for="(item , index) in pedigreeauthList" :key="'item'+index" :data-index="index">
				<view class="type-view"  @click="clickShuoMing">
					<text class="list-type" style="color: #FF4206;">{{item.type}}</text>
					<text class="list-type-desc">{{item.typeDesc}}</text>
					<image class="list-type-icon" src="../../static/fan_png_ic_question_mark.png"></image>
				</view>
				<view class="list-view" v-for="(innerItem , innerIndex) in item.list" :key="'item'+innerIndex" :data-index="innerItem">
					<text class="name-txt">{{innerItem.name}}({{innerItem.mobile}})</text>
					<view v-if="item.type!=='移除记录'" style="display: flex;">
						<text class="dingwei-txt" @click="dingwei" v-if="!innerItem.CardId" :data-index="innerItem">定位</text>
						<text class="dingwei-txt" v-else style="background: #E2E2E2;" @click="unDingWei" :data-index="innerItem">已定位</text>
						<text v-if="innerItem.PQD!==userPQD" class="quanxian-txt" @click="setQuanxian" :data-index="innerItem">权限</text>
						<text v-else class="quanxian-txt" style="background-color: #FFFFFF;"></text>
					</view>
					<text v-else class="quanxian-txt" @click="setHuiFu" :data-index="innerItem">恢复</text>
				</view>
				<view style="background-color: #F5F6FA;width: 100%;height: 1rpx;"></view>
			</view>
		</scroll-view>
		<setPermissionsPop v-if="isShowSetQuanXian" @returnQuanXian="changeQuanxian" />
		<inputTBDalog :isShow="isShowUnBinding" :dataSource="dialogData" @returnInputDialog="returnUnBinding" />
	</view>
</template>

<script>
	import request from '../index/request.js';
	import setPermissionsPop from "@/components/set-permissions-pop/set-permissions-pop.vue";
	import inputTBDalog from '../../components/tip-two-btn-dialog/tip-two-btn-dialog.vue'
	let app = getApp();

	export default {
		components: {
			setPermissionsPop,
			inputTBDalog
		},
		data() {
			return {
				isiPhoneX: false,
				pedigreeauthList: [],
				treeId: "",
				isShowSetQuanXian: false,
				isShowUnBinding: false,
				jiapuInfo: {},
				selectQuanXianData: {},
				dialogData: {
					title: "温馨提示",
					content: "是否将此人在树谱中解除绑定？",
					leftTxt: "取消",
					rightTxt: "确定",
					tag: "zhiye",
				},
				userAuth: 0,
				userPQD: "",
			}
		},
		onUnload: function() {
			uni.$off("refrashTree")
		},
		onLoad: function(options) {
			this.jiapuInfo = uni.getStorageSync("jiapu_click_data")
			var user = uni.getStorageSync("shiqin-user")
			this.userPQD = user.PQD
			this.treeId = this.jiapuInfo.id
			this.getPedigreeauthList()
			this.isiPhoneX = app.globalData.isIphone
			console.log(this.isiPhoneX)
			// 监听事件
			uni.$on('refrashTree', this.getPedigreeauthList);
		},
		methods: {
			onShow: function() {
				this.share.url = '/pages/tabBar/home/index?appInviteId=' + this.appUtil.getAppInviteId()
				this.share.shareType = 1
			},
			getPedigreeauthList: function() {
				var that = this
				this.pedigreeauthList.splice(0, this.pedigreeauthList.length);
				request.post("api/common/get_pedigreeauth_list", {
					p_id: that.treeId
				}).then(data => {
					that.userAuth = data.is_auth
					//3发起人 2联合发起人 1管理员 0成员 4仅查看
					var item1 = {
						type: "发起人",
						// typeDesc: "（本家谱创建人，拥有最高权限）",
						list: []
					}
					var item2 = {
						type: "联合发起人",
						// typeDesc: "（权限仅次于发起人的权限）",
						list: []
					}
					var item0 = {
						type: "财务",
						// typeDesc: "（家谱编委会财务权限）",
						list: []
					}
					var item3 = {
						type: "管理员",
						// typeDesc: "（管理成员及以下的人的权限）",
						list: []
					}
					var item4 = {
						type: "成员",
						// typeDesc: "（在树谱中能添加亲人）",
						list: []
					}
					var item5 = {
						type: "仅查看",
						// typeDesc: "（只能浏览查看家谱信息）",
						list: []
					}
					var item6 = {
						type: "移除记录",
						list: []
					}
					for (var i = 0; i < data.datas.length; i++) {
						if (data.datas[i].is_finance == 1) {
							item0.list.push(data.datas[i])
						}
						switch (data.datas[i].auth) {
							case 2:
								item2.list.push(data.datas[i])
								break
							case 3:
								item1.list.push(data.datas[i])
								break
							case 1:
								item3.list.push(data.datas[i])
								break
							case 0:
								item4.list.push(data.datas[i])
								break
							case 4:
								item5.list.push(data.datas[i])
								break
						}
					}
					for (var i = 0; i < data.dataslog.length; i++) {
						item6.list.push(data.dataslog[i])
					}
					if (item1.list.length > 0) {
						that.pedigreeauthList.push(item1)
					}
					if (item2.list.length > 0) {
						that.pedigreeauthList.push(item2)
					}
					if (item0.list.length > 0) {
						that.pedigreeauthList.push(item0)
					}
					if (item3.list.length > 0) {
						that.pedigreeauthList.push(item3)
					}
					if (item4.list.length > 0) {
						that.pedigreeauthList.push(item4)
					}
					if (item5.list.length > 0) {
						that.pedigreeauthList.push(item5)
					}
					if (item6.list.length > 0) {
						that.pedigreeauthList.push(item6)
					}
				})
			},
			setHuiFu: function(event) {
				let item = event.currentTarget.dataset.index;
				if (this.userAuth == 1 || this.userAuth == 2 || this.userAuth == 3) {
					this.delPedigreeLog(item.id)
				} else {
					uni.showToast({
						title: "您还不是管理员，请联系上方管理员或发起人",
						icon: "none"
					})
				}
			},
			setQuanxian: function(event) {
				if (this.userAuth == 1 || this.userAuth == 2 || this.userAuth == 3) {
					this.isShowSetQuanXian = true
					let item = event.currentTarget.dataset.index;
					this.selectQuanXianData = item
				} else {
					uni.showToast({
						title: "您还不是管理员，请联系上方管理员或发起人",
						icon: "none"
					})
				}
			},
			changeQuanxian: function(e) {
				this.isShowSetQuanXian = false
				switch (e.index) {
					case 3: //想设置为联合发起人
						this.setPedigreeauth(2)
						break
					case 1: //想设置为管理员
						this.setPedigreeauth(1)
						break
					case 0: //想设置为家谱成员
						this.setPedigreeauth(0)
						break
					case 4: //想设置为仅查看
						this.setPedigreeauth(5)
						break
					case 5: //想设置为移除家谱
						this.setPedigreeauth(4)
						break
				}
			},
			dingwei: function(event) {
				//3发起人 2联合发起人 1管理员 0成员 4仅查看
				if (this.userAuth == 1 || this.userAuth == 2 || this.userAuth == 3) {
					let item = event.currentTarget.dataset.index;
					uni.navigateTo({
						url: "../change-position/index?data=" + JSON.stringify(item)
					})
				} else {
					uni.showToast({
						title: "您还不是管理员，请联系上方管理员或发起人",
						icon: "none"
					})
				}
			},
			unDingWei: function(event) {
				//3发起人 2联合发起人 1管理员 0成员 4仅查看
				if (this.userAuth == 1 || this.userAuth == 2 || this.userAuth == 3) {
					this.isShowUnBinding = true
					let item = event.currentTarget.dataset.index;
					this.selectQuanXianData = item
				} else {
					uni.showToast({
						title: "您还不是管理员，请联系上方管理员或发起人",
						icon: "none"
					})
				}
			},
			returnUnBinding: function(e) {
				this.isShowUnBinding = e.isclose
				if (e.tag) {
					this.unbindTreeCard()
				}
			},
			delPedigreeLog: function(id) {
				var that = this
				request.post("api/common/DelPedigreeLog", {
					id: id
				}).then(data => {
					that.getPedigreeauthList()
				})
			},
			unbindTreeCard: function(auth_val) {
				var that = this
				request.post("api/apiv2/NewUnbindTreeCard", {
					OnPQD: this.selectQuanXianData.PQD,
					TreeId: this.treeId
				}).then(data => {
					that.getPedigreeauthList()
				})
			},
			setPedigreeauth: function(auth_val) {
				var that = this
				request.post("api/common/set_pedigreeauth", {
					id: this.selectQuanXianData.id,
					auth_val: auth_val
				}).then(data => {
					that.getPedigreeauthList()
				})
			},
			clickShuoMing: function() {
				if (this.userAuth == 1 || this.userAuth == 2 || this.userAuth == 3) {
					var that = this
					request.post("api/common/H5_page_v2", {
						id: "69"
					}).then(data => {
						uni.navigateTo({
							url: '/pages/article-details/index?load_url=' +
								encodeURIComponent(data.url) + "&title=" + data.name
						})
					})
				} else {
					uni.showToast({
						title: "您还不是管理员，请联系上方管理员或发起人",
						icon: "none"
					})
				}
			},
		}
	}
</script>

<style>
	@import "index.css";
</style>
